package com.wx.business.trade.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.eachonline.core.Pagination;
import com.eachonline.mapper.GenericMapper;
import com.eachonline.service.GenericServiceImpl;
import com.eachonline.util.RedisHelper;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.business.course.OrderRecordService;
import com.wx.business.trade.IxbTradeRecordService;
import com.wx.business.trade.WXPushMessageService;
import com.wx.business.util.OrderStatus;
import com.wx.business.util.PayTypeStatus;
import com.wx.business.util.TradeNoGenerater;
import com.wx.business.util.TradeStatus;
import com.wx.business.util.TradeType;
import com.wx.dao.mapper.IxbTradeRecordMapper;
import com.wx.model.AppConfig;
import com.wx.model.IxbCourseDetail;
import com.wx.model.IxbTradeRecord;
import com.wx.model.IxbTradeRecordExample;
import com.wx.model.IxbUser;
@Service("ixbTradeRecordService")
public class IxbTradeRecordServiceImpl extends GenericServiceImpl<IxbTradeRecord, IxbTradeRecordExample, String> implements IxbTradeRecordService  {

	private static final  Logger logger =Logger.getLogger(IxbTradeRecordServiceImpl.class);

	@Resource
	private IxbTradeRecordMapper ixbTradeRecordMapper;
	
	@Resource
	private OrderRecordService orderRecordService;
	
	@Resource
	private IxbCourseDetailService ixbCourseDetailService;
	
	@Resource
	private WXPushMessageService wXPushMessageService;
	
	@Override
	protected GenericMapper<IxbTradeRecord, IxbTradeRecordExample, String> getGenericMapper() {
		return ixbTradeRecordMapper;
	}

	@Override
	public void afterTradeStatus(String tradeNo,String thirdOrderNo) throws Exception {
		// TODO Auto-generated method stub
		IxbTradeRecordExample ext=new IxbTradeRecordExample();
		ext.createCriteria().andTradeNoEqualTo(tradeNo);
		List<IxbTradeRecord> tradeList=ixbTradeRecordMapper.selectByExample(ext);
		if(tradeList.isEmpty()||tradeList.size()<1)
			throw new Exception("交易记录不存在");
		
		IxbTradeRecord  tradeRecord=tradeList.get(0);
		tradeRecord.setTradeStatus(TradeStatus.TRADE_SUCESS);
		tradeRecord.setFinishTime(new Date());
		ixbTradeRecordMapper.updateByPrimaryKey(tradeRecord);
		
		IxbCourseDetail courseDetail= ixbCourseDetailService.selectByPrimaryKey(tradeRecord.getCourseId());
		courseDetail.setSalesVolume(courseDetail.getSalesVolume()+1);
		ixbCourseDetailService.updateByPrimaryKey(courseDetail);
		
		//改变订单状态
		orderRecordService.changeOrderStatus(tradeRecord.getOrderNo(), tradeRecord.getUserId(), OrderStatus.ORDER_SUCESS);
		
	    IxbUser user=(IxbUser) RedisHelper.getObject(tradeRecord.getUserId());
	    SimpleDateFormat  sdf=new SimpleDateFormat("yyyy年mm月dd日");
		Map<String,Object> pushData=new HashMap<String, Object>();
		pushData.put("first", "恭喜你购买课程成功!");
		pushData.put("keyword1", tradeRecord.getTradeNo());
		pushData.put("keyword2", tradeRecord.getCourseName());
		pushData.put("keyword3", tradeRecord.getTradeFee());
		pushData.put("keyword4", thirdOrderNo);
		pushData.put("keyword5", sdf.format(tradeRecord.getFinishTime()));
		pushData.put("remark", "谢谢您的光临!");
		wXPushMessageService.pushMessage(user.getOpenid(), 1, pushData);
		
	}

	@Override
	public IxbTradeRecord createTradeRecord(String buyerId, String buyerName,
			String courseId, String courseName, double tradeFee,String orderNo)
			throws Exception {
		// TODO Auto-generated method stub
		IxbTradeRecord record=new IxbTradeRecord();
    	String tradeNo=TradeNoGenerater.getInstance().generaterPayTradeNo();
    	record.setUserId(buyerId);
    	record.setUserName(buyerName);
    	record.setCourseId(courseId);
    	record.setCourseName(courseName);
    	record.setCreateTime(new Date());
    	record.setOrderNo(orderNo);
    	record.setPayType(PayTypeStatus.ZF_WX);
    	record.setTradeFee(tradeFee);
    	record.setTradeNo(tradeNo);
    	record.setTradeStatus(TradeStatus.TRADE_PAY);
    	record.setTradeType(TradeType.JZ_ZXZF);
    	getGenericMapper().insert(record);
    	return record;
	}

	@Override
	public Pagination<Map<String, Object>> queryCommTradeRecordByPage(
			int start, int pageSize, IxbTradeRecordExample ext) {
		// TODO Auto-generated method stub
		Pagination<Map<String, Object>> pageInfo=new Pagination<Map<String,Object>>(start, pageSize);
		ext.setPagination(pageInfo);
		ext.setTradeType(3);
		ext.setOrderByClause(" create_time desc");;
		List<Map<String, Object>> data= ixbTradeRecordMapper.queryCommissonListByExample(ext);
		
		for (Map<String, Object> detail:data) {
	    	detail.put("businessPicUrl", AppConfig.BaseUrl+detail.get("businessPic"));
		}
		
		ext.createCriteria().andUserIdEqualTo(ext.getUserId()).andTradeTypeEqualTo(3);
		int count=ixbTradeRecordMapper.queryCommissonCountByExample(ext);
		pageInfo.setDataList(data);
		pageInfo.setTotalRowNumber(count);
		return pageInfo;
	}

	
}
